// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var css_detail_info = require("./memberGradeaudit.css");
	var constant = require("constant");
	var html_auditGrade = require("./html_auditGrade.html");
	var html_auditGrade_detail = require("./clientManage_auditStateDetail.html");
	var enterpriseId;
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var init = function() {
		inquireEnterpriseGradeEvaluation();
		initEvent();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}
		$(".leftMenu-item").removeClass("leftMenu-active");
		$(".memberGradeAduitList").addClass("leftMenu-active");
	     //搜索按钮
	    $(".table-search-button").on("click",function(){
	        pageIndex = 1;
			pageSize = 10;
			totalPageCount = 1;
			totalRecordCount = 0; //分页初始化
			inquireEnterpriseGradeEvaluation();
	    });
	};

	//查询资信等级调整记录
	var inquireEnterpriseGradeEvaluation = function() {
		base.fnAjax({
			action: "inquireEnterpriseGradeEvaluation.json",
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				enterpriseName:$("#memberGradeaudit input[name=enterpriseName]").val(),
				auditState:$("#memberGradeaudit select[name=auditState]").val()
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					juicer.register("getValueByKey", constant.getValueByKey);
					var tpl = $("#memberGradeTpl").html();
					var html = juicer(tpl, data);
					$("#memberGradeTable").html(html);
					
					$(".consent").unbind("click").click(function(){
						var gradeEvaluationId = $(this).parents("tr").attr("gradeEvaluationId");
						$("#popdialog").html(html_auditGrade);
						$("#audit_grade_dialog").attr("action", "consentEnterpriseGradeEvaluation");
						$("#audit_grade_dialog #gradeEvaluationId").val(gradeEvaluationId);
						$("#audit_grade_dialog .dialogTitle").html("审核通过");
						$("#audit_grade_dialog .contentTip").val("是否确认通过该条记录？");
						dialog.showDialog($("#audit_grade_dialog"));
						
						$("#audit_grade_dialog .dialogHandlerBtn").unbind("click").click(function(){
							databind.submitDom($("#audit_grade_dialog"), function(){
								base.alert(1, "同意成功！");
								dialog.hideDialog($("#audit_grade_dialog"));
								router.route("client/memberGradeaudit.html");
							});
						});
						
						$("#audit_grade_dialog .dialogCancelBtn").unbind("click").click(function(){
							dialog.hideDialog($("#audit_grade_dialog"));
						});
					});
					
					$(".oppose").unbind("click").click(function(){
						var gradeEvaluationId = $(this).parents("tr").attr("gradeEvaluationId");
						$("#popdialog").html(html_auditGrade);
						$("#audit_grade_dialog").attr("action", "opposeEnterpriseGradeEvaluation");
						$("#audit_grade_dialog #gradeEvaluationId").val(gradeEvaluationId);
						$("#audit_grade_dialog .dialogTitle").html("审核拒绝");
						$("#audit_grade_dialog .contentTip").val("是否确认拒绝该条记录？");
						dialog.showDialog($("#audit_grade_dialog"));
						$("#audit_grade_dialog .dialogHandlerBtn").unbind("click").click(function(){
							databind.submitDom($("#audit_grade_dialog"), function(){
								base.alert(1, "拒绝成功！");
								dialog.hideDialog($("#audit_grade_dialog"));
								router.route("client/memberGradeaudit.html");
							});
						});
						
						$("#audit_grade_dialog .dialogCancelBtn").unbind("click").click(function(){
							dialog.hideDialog($("#audit_grade_dialog"));
						});
					});
					
					$("#memberGradeaudit .detail").unbind("click").click(function(){
						router.route("client/enterpriseAudit.html?enterpriseId=" + $(this).parents("tr").attr("enterpriseId") + "&gradeEvaluationId=" + $(this).parents("tr").attr("gradeEvaluationId"));
					});
					
					totalPageCount = data.totalPageCount;
                    totalRecordCount = data.totalRecordCount;
                    base.pageControl(
                    	$("#memberGradeaudit .pagerCon"),
                        totalRecordCount,
                        data.pageIndex,
                        data.pageSize,
                        function(p) {
                            pageIndex = p;
                            inquireEnterpriseGradeEvaluation();
                    });
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	
	return {
		init: init
	};
});